package ui;

import service.IUserService;
import service.UserService;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

//import dao.DaoImpl;
//import dao.DaoProxy;
import dao.IUserDAO;
import service.IUserService;
import service.UserService;
import model.User;
public class MainFrame extends JDialog{
	String[] heads={"UserId","UserName","UserPassword"};
	Object[][] rows=null;
	DefaultTableModel model=new DefaultTableModel(rows,heads);;
	JTable userTable=new JTable(model);;
	JScrollPane scrollPane= new JScrollPane(userTable);
	private JPanel p1;
	private JButton update,delete,updateList,exit;
	public MainFrame() throws Exception{
		update=new JButton("修改密码");
		delete=new JButton("删除用户");
		updateList=new JButton("刷新");
		exit=new JButton("退出");
		p1=new JPanel();
		p1.add(update);
		p1.add(delete);
		p1.add(updateList);
		p1.add(exit);
		setLayout(new BorderLayout(20,15));
		add(p1,BorderLayout.NORTH);
		add(scrollPane);
		add(new JLabel("可选中其中一列进行修改或删除"),BorderLayout.SOUTH);

		IUserService service=new UserService();
		List<User> list=service.findAll(); 
		Iterator<User> all = list.iterator();
		int len=list.size();//记录表的行数
		rows=new Object[len][3];
		len=0;
		while(all.hasNext()){
			User user=all.next(); 
			rows[len][0]=user.getId(); 
			rows[len][1]=user.getname();
			rows[len][2]=user.getpassword();	
			model.addRow(rows[len]);
			len++;
		}
		setTitle("用户信息");
  	    setSize(500,400); 
  	    setLocation(500,250);
  	    setLocationRelativeTo(null);
  	  update.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int n=-1;
				int x=userTable.getSelectedRowCount();//判断是否选中一行
				if(x!=1)
					JOptionPane.showMessageDialog(null, "请选中表格某一行！","提醒",JOptionPane.ERROR_MESSAGE);
				else{
					n=userTable.getSelectedRow();//记录选中的行数
					if(n>=0){
						try {
							User user=new User();
							user.setId((int)rows[n][0]);
							user.setname((String)rows[n][1]);
							UpdateUsers upd=new UpdateUsers();
							upd.setUser(user);
							upd.setVisible(true);
							upd.name.setText(user.getname());
						} catch (Exception e1) {
							e1.printStackTrace();
						}	
				}
				}
			}	
		});

  	delete.addActionListener(new ActionListener(){
		@SuppressWarnings("null")
		public void actionPerformed(ActionEvent e){
			int n=-1;
			int x=userTable.getSelectedRowCount();//判断是否选中一行
			if(x!=1)
				JOptionPane.showMessageDialog(null, "请选中表格某一行！","提醒",JOptionPane.ERROR_MESSAGE);
			else{
				n=userTable.getSelectedRow();//记录选中的行数
				if(n>=0){
					try {
					String name =(String)rows[n][1];
					int option =JOptionPane.YES_OPTION;
					option=JOptionPane.showConfirmDialog(null, "是否删除用户?   "+name);
					if(option==JOptionPane.YES_OPTION){
						User user=new User();
							user.setId((int)rows[n][0]);
							UserService service=new UserService();
							if(service.deleteUser(user))
							JOptionPane.showMessageDialog(null, "该用户删除成功！","删除成功",JOptionPane.INFORMATION_MESSAGE);						
					}
				}catch (Exception e1) {
						e1.printStackTrace();}
				}}
		}
		});

  	 updateList.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				setVisible(false);
				MainFrame frame=null;
				try {
					frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
			});
  	 
  	exit.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
			setVisible(false);
			new LoginDialog();
		}
	});
	}
	public static void mainFrame() throws Exception{
		MainFrame n =  new MainFrame();
		n.setVisible(true);
	}

}

